﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;
using System.Web.UI.HtmlControls;

namespace BladeTecWebApplication.Admin
{
	public partial class PrintAttempt : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				int attempt_id;
				GetAttemptDetail_Result [] attempt_detail;
				QUIZ_ATTEMPT attempt;
				NetSysData.NetSysData data = new NetSysData.NetSysData();
				GetAttemptList_Result[] attempts = data.GetAttemptList();

				try
				{
					attempt_id = Int32.Parse(Request.QueryString["attempt_id"]);
				}
				catch (Exception)
				{
					// show error
					return;
				}

				attempt_detail = data.GetQuizAttemptDetail(attempt_id);
				if (attempt_detail == null)
				{
					//show error
					return;
				}

				attempt = data.GetQuizAttempt(attempt_id);
				if (attempt == null)
				{
					//show error
					return;
				}

				// Fill in attempt information.
				lblApplicantName.Text = attempt.APPLICANT.FIRST_NAME + " " + attempt.APPLICANT.LAST_NAME;
				lblAttemptDate.Text = DateTime.Parse(attempt.ATTEMPT_DATE.ToString()).ToString("d");
				lblGraded.Text = (attempt.GRADED == 1) ? "Yes" : "No";

				double grade;
				foreach (GetAttemptList_Result result in attempts)
				{
					if (result.ATTEMPT_ID == attempt_id)
					{
						grade = Double.Parse(result.CORRECT.ToString()) / (Double.Parse(result.CORRECT.ToString()) + Double.Parse(result.INCORRECT.ToString()));
						grade = grade*100;
						lblScore.Text = grade.ToString() + "%";
						break;
					}
				}

				// Quiz numbering.
				int i = 1;
				foreach (GetAttemptDetail_Result result in attempt_detail)
				{
					HtmlTableRow row = new HtmlTableRow();
					HtmlTableCell cell1 = new HtmlTableCell();
					HtmlTableCell cell2 = new HtmlTableCell();
					HtmlTableCell cell3 = new HtmlTableCell();
					HtmlTableCell cell4 = new HtmlTableCell();
					HtmlTableCell cell5 = new HtmlTableCell();

					// Question Number
					cell1.VAlign = "top";
					cell1.Style.Add("width", "10px");
					cell1.InnerHtml = i.ToString();
					row.Cells.Add(cell1);

					// Question Text
					cell2.VAlign = "top";
					cell2.InnerHtml = result.QUESTION_TEXT;
					cell2.Style.Add("padding-left", "10px");
					cell2.Style.Add("width", "400px");
					row.Cells.Add(cell2);

					// Question Type
					cell3.VAlign = "top";
					cell3.Width = "60";
					cell3.NoWrap = true;
					cell3.InnerHtml = (result.QUESTION_TYPE == "M") ? "Multiple Choice" : "Short Answer";
					row.Cells.Add(cell3);

					// Applicant Response
					if (result.QUESTION_TYPE == "M")
					{
						cell4.InnerHtml = "N/A";
					}
					else
					{
						cell4.InnerHtml = result.ANSWER_TEXT;
					}
					cell4.VAlign = "top";
					row.Cells.Add(cell4);

					cell5.InnerHtml = (result.ANSWERED_CORRECTLY == 1) ? "Yes" : "No";

					row.Cells.Add(cell5);

					if (i % 2 == 0)
					{
						row.Attributes.Add("class", "netsysTableRowAlt");
					}
					else
					{
						row.Attributes.Add("class", "netsysTableRow");
					}
					tblAttemptDetail.Rows.Add(row);

					i++;
				}

			}
		}

	}
}